package com.kzd.gtgame.main.mine

import androidx.lifecycle.LifecycleOwner
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.custom.setTransitionStatus
import com.kzd.gtgame.entity.Game
import com.kzd.gtgame.entity.TransferInfo
import com.kzd.gtgame.entity.TransferRole
import com.kzd.gtgame.jetpack.vm.MineViewModel
import com.kzd.gtgame.main.KEY_GAME_DETAILS_ID
import com.kzd.gtgame.main.popup.GameTransferPopup
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_transfer_apply.*

class TransferApplyActivity : OwnerViewModeActivity<MineViewModel>() {

    private val mGameTransferPopup: GameTransferPopup by lazy {
        GameTransferPopup(context = this, onGameList = { _, game ->
            mGameTransferPopup.dismiss()
            this.mTransferGame = game
            this.mTransferRole0 = null
            tvTransferApplyGameName.text = game.name
            tvTransferApplyRoleName.text = null
        }, onRoleList = { _, role ->
            mGameTransferPopup.dismiss()
            if (mRoleType == 0) {
                this.mTransferRole0 = role
                tvTransferApplyRoleName.text = role.role_name
            } else {
                this.mTransferRole1 = role
                tvTransferApplyToRoleName.text = role.role_name
            }
        })
    }

    private val mGameId: String
        get() = intent?.extras?.getString(KEY_GAME_DETAILS_ID) ?: "0"

    private var mTransferGame: Game? = null
    private var mTransferRole0: TransferRole? = null
    private var mTransferRole1: TransferRole? = null

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_transfer_apply

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = flTransferApplyBar)
    }

    override fun initView() {
        tvTransferApplyGameName.setOnClickAntiShakeListener {
            val games = viewModel.transferInfoLiveData.value?.data?.data?.games
            if (games.isNullOrEmpty()) {
                toast("暂无游戏可选择")
            } else {
                mGameTransferPopup.fillDataGame(games)
                    .showPopupWindow()
            }
        }

        tvTransferApplyRoleName.setOnClickAntiShakeListener {
            val game = mTransferGame
            if (game == null) {
                toast("请选择游戏")
            } else {
                viewModel.mineTransferRole(game.appid)
                mRoleType = 0
            }
        }

        tvTransferApplyToArea.setOnClickAntiShakeListener {
            val servers = viewModel.transferInfoLiveData.value?.data?.data?.servers
            if (servers.isNullOrEmpty()) {
                toast("暂无区服可选择")
            } else {

            }
        }


        tvTransferApplyToRoleName.setOnClickAntiShakeListener {
            val gamesAppid = viewModel.transferInfoLiveData.value
                ?.data?.data?.appid ?: return@setOnClickAntiShakeListener
            viewModel.mineTransferRole(gamesAppid)
            mRoleType = 1
        }

        tvTransferApplySubmit.setOnClickAntiShakeListener {
            if (mTransferGame == null) {
                toast("请选择转出游戏名称")
                return@setOnClickAntiShakeListener
            }
            if (mTransferRole0 == null) {
                toast("请选择转出游戏角色")
                return@setOnClickAntiShakeListener
            }
            if (mTransferRole1 == null) {
                toast("请选择转入游戏角色")
                return@setOnClickAntiShakeListener
            }
            val gid: String = mTransferGame?.id ?: return@setOnClickAntiShakeListener
            val roleid: String = mTransferRole0?.id?.toString() ?: return@setOnClickAntiShakeListener
            val turn_gid: String = mGameId
            val turn_roleid: String = mTransferRole1?.id?.toString() ?: return@setOnClickAntiShakeListener
            viewModel.mineTransferApply(gid, roleid, turn_gid, turn_roleid)
        }
    }

    override fun initData() {
        viewModel.mineTransferInfo(mGameId)
    }

    private var mRoleType: Int = 0

    override fun initViewMode() {
        viewModel.transferInfoLiveData.observe(this as LifecycleOwner, { response ->
            mslMultiStatusLayout.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            fillData(data)
        })

        viewModel.transferRoleLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            mGameTransferPopup.fillDataRole(data)
                .showPopupWindow()
        })
        viewModel.transferApplyLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            toast(response.data?.message)
            finish()
        })

    }

    private fun fillData(transferInfo: TransferInfo) {
        tvTransferApplyAccount.text = transferInfo.user_name
        tvTransferApplyToAccount.text = transferInfo.user_name
        tvTransferApplyToGameName.text = transferInfo.game_name
    }
}